package com.cookor.poi.modules.phy.schedule;

import com.cookor.poi.common.utils.DateUtils;
import com.cookor.poi.common.utils.JedisUtils;
import com.cookor.poi.modules.phy.dao.TOrderDao;
import com.cookor.poi.modules.phy.entity.FSchedue;
import com.cookor.poi.modules.phy.entity.TOrder;
import com.cookor.poi.modules.phy.service.FSchedueService;
import com.cookor.poi.modules.phy.service.FSchedueTemplateService;
import com.cookor.poi.modules.phy.util.DequeUtil;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

public class AppointSchedule {
  private static final Logger logger = Logger.getLogger(ShiftSchedule.class);
  
  public static Map<Integer, Integer> bookNoMap;
  
  @Autowired
  private FSchedueTemplateService fSchedueTemplateService;
  
  @Autowired
  private FSchedueService fSchedueService;
  
  @Autowired
  private TOrderDao tOrderDao;
  
  public void resetCancelThreeListSche() throws Exception {
    resetCancelThreeList();
  }
  
  public void reset() throws Exception {
    Calendar now = Calendar.getInstance();
    int hour = now.get(11);
    if (hour == 1);
  }
  
  public void resetAppointedListSche() throws Exception {
    Calendar now = Calendar.getInstance();
    int hour = now.get(11);
    if (hour == 1)
      DequeUtil.resetDeque(); 
    String year = DateUtils.getYear();
    String month = DateUtils.getMonth();
    String day = DateUtils.getDay();
    Date today = DateUtils.parseDate(year + "-" + month + "-" + day, new String[] { "yyyy-MM-dd" });
    FSchedue fSchedue = new FSchedue();
    fSchedue.setScheduleYear(Integer.valueOf(Integer.parseInt(year)));
    fSchedue.setScheduleMonth(Integer.valueOf(Integer.parseInt((month.indexOf("0") == 0) ? month.replace("0", "") : month)));
    fSchedue.setScheduleDay(Integer.valueOf(Integer.parseInt((day.indexOf("0") == 0) ? day.replace("0", "") : day)));
    List<FSchedue> list = this.fSchedueService.findList(fSchedue);
    FSchedue schedue = list.get(0);
    Integer countA = schedue.getCountA();
    Integer countB = schedue.getCountB();
    Integer countC = schedue.getCountC();
    Integer countD = schedue.getCountD();
    Integer countE = schedue.getCountE();
    if (countA != null && countA.intValue() > 0 && DequeUtil.Asize(Integer.valueOf(1)) != countA) {
      TOrder orderQuery = new TOrder();
      orderQuery.setBookDate(today);
      orderQuery.setBookDateType(Integer.valueOf(1));
      orderQuery.setTenantId(Integer.valueOf(5));
      List<TOrder> orderList = this.tOrderDao.findList(orderQuery);
      for (int i = orderList.size() + 1; i < countA.intValue() + orderList.size() + 1; i++)
        DequeUtil.addA(Integer.valueOf(1), Integer.valueOf(i)); 
    } 
    if (countB != null && countB.intValue() > 0 && DequeUtil.Asize(Integer.valueOf(2)) != countB) {
      TOrder orderQuery = new TOrder();
      orderQuery.setBookDate(today);
      orderQuery.setBookDateType(Integer.valueOf(2));
      orderQuery.setTenantId(Integer.valueOf(5));
      List<TOrder> orderList = this.tOrderDao.findList(orderQuery);
      for (int i = orderList.size() + 1; i < countB.intValue() + orderList.size() + 1; i++)
        DequeUtil.addA(Integer.valueOf(2), Integer.valueOf(i)); 
    } 
    if (countC != null && countC.intValue() > 0 && DequeUtil.Asize(Integer.valueOf(3)) != countC) {
      TOrder orderQuery = new TOrder();
      orderQuery.setBookDate(today);
      orderQuery.setBookDateType(Integer.valueOf(3));
      orderQuery.setTenantId(Integer.valueOf(5));
      List<TOrder> orderList = this.tOrderDao.findList(orderQuery);
      for (int i = orderList.size() + 1; i < countC.intValue() + orderList.size() + 1; i++)
        DequeUtil.addA(Integer.valueOf(3), Integer.valueOf(i)); 
    } 
    if (countD != null && countD.intValue() > 0 && DequeUtil.Asize(Integer.valueOf(4)) != countD) {
      TOrder orderQuery = new TOrder();
      orderQuery.setBookDate(today);
      orderQuery.setBookDateType(Integer.valueOf(4));
      orderQuery.setTenantId(Integer.valueOf(5));
      List<TOrder> orderList = this.tOrderDao.findList(orderQuery);
      for (int i = orderList.size() + 1; i < countD.intValue() + orderList.size() + 1; i++)
        DequeUtil.addA(Integer.valueOf(4), Integer.valueOf(i)); 
    } 
    if (countE != null && countE.intValue() > 0 && DequeUtil.Asize(Integer.valueOf(5)) != countE) {
      TOrder orderQuery = new TOrder();
      orderQuery.setBookDate(today);
      orderQuery.setBookDateType(Integer.valueOf(5));
      orderQuery.setTenantId(Integer.valueOf(5));
      List<TOrder> orderList = this.tOrderDao.findList(orderQuery);
      for (int i = orderList.size() + 1; i < countE.intValue() + orderList.size() + 1; i++)
        DequeUtil.addA(Integer.valueOf(5), Integer.valueOf(i)); 
    } 
    Date tomorrow = DateUtils.addDays(today, 1);
    fSchedue = new FSchedue();
    fSchedue.setScheduleYear(Integer.valueOf(Integer.parseInt(year)));
    fSchedue.setScheduleMonth(Integer.valueOf(Integer.parseInt((month.indexOf("0") == 0) ? month.replace("0", "") : month)));
    fSchedue.setScheduleDay(Integer.valueOf(Integer.parseInt((day.indexOf("0") == 0) ? day.replace("0", "") : day) + 1));
    list = this.fSchedueService.findList(fSchedue);
    schedue = list.get(0);
    countA = schedue.getCountA();
    countB = schedue.getCountB();
    countC = schedue.getCountC();
    countD = schedue.getCountD();
    countE = schedue.getCountE();
    if (countA != null && countA.intValue() > 0 && DequeUtil.Bsize(Integer.valueOf(1)) != countA) {
      TOrder orderQuery = new TOrder();
      orderQuery.setBookDate(tomorrow);
      orderQuery.setBookDateType(Integer.valueOf(1));
      orderQuery.setTenantId(Integer.valueOf(5));
      List<TOrder> orderList = this.tOrderDao.findList(orderQuery);
      for (int i = orderList.size() + 1; i < countA.intValue() + orderList.size() + 1; i++)
        DequeUtil.addB(Integer.valueOf(1), Integer.valueOf(i)); 
    } 
    if (countB != null && countB.intValue() > 0 && DequeUtil.Bsize(Integer.valueOf(2)) != countB) {
      TOrder orderQuery = new TOrder();
      orderQuery.setBookDate(tomorrow);
      orderQuery.setBookDateType(Integer.valueOf(2));
      orderQuery.setTenantId(Integer.valueOf(5));
      List<TOrder> orderList = this.tOrderDao.findList(orderQuery);
      for (int i = orderList.size() + 1; i < countB.intValue() + orderList.size() + 1; i++)
        DequeUtil.addB(Integer.valueOf(2), Integer.valueOf(i)); 
    } 
    if (countC != null && countC.intValue() > 0 && DequeUtil.Bsize(Integer.valueOf(3)) != countC) {
      TOrder orderQuery = new TOrder();
      orderQuery.setBookDate(tomorrow);
      orderQuery.setBookDateType(Integer.valueOf(3));
      orderQuery.setTenantId(Integer.valueOf(5));
      List<TOrder> orderList = this.tOrderDao.findList(orderQuery);
      for (int i = orderList.size() + 1; i < countC.intValue() + orderList.size() + 1; i++)
        DequeUtil.addB(Integer.valueOf(3), Integer.valueOf(i)); 
    } 
    if (countD != null && countD.intValue() > 0 && DequeUtil.Bsize(Integer.valueOf(4)) != countD) {
      TOrder orderQuery = new TOrder();
      orderQuery.setBookDate(tomorrow);
      orderQuery.setBookDateType(Integer.valueOf(4));
      orderQuery.setTenantId(Integer.valueOf(5));
      List<TOrder> orderList = this.tOrderDao.findList(orderQuery);
      for (int i = orderList.size() + 1; i < countD.intValue() + orderList.size() + 1; i++)
        DequeUtil.addB(Integer.valueOf(4), Integer.valueOf(i)); 
    } 
    if (countE != null && countE.intValue() > 0 && DequeUtil.Bsize(Integer.valueOf(5)) != countE) {
      TOrder orderQuery = new TOrder();
      orderQuery.setBookDate(tomorrow);
      orderQuery.setBookDateType(Integer.valueOf(5));
      orderQuery.setTenantId(Integer.valueOf(5));
      List<TOrder> orderList = this.tOrderDao.findList(orderQuery);
      for (int i = orderList.size() + 1; i < countE.intValue() + orderList.size() + 1; i++)
        DequeUtil.addB(Integer.valueOf(5), Integer.valueOf(i)); 
    } 
    logger.info("排号初始化成功！");
    logger.info(DequeUtil.deque1_A.toString());
    logger.info(DequeUtil.deque2_A.toString());
    logger.info(DequeUtil.deque3_A.toString());
    logger.info(DequeUtil.deque4_A.toString());
    logger.info(DequeUtil.deque5_A.toString());
    logger.info(DequeUtil.deque1_B.toString());
    logger.info(DequeUtil.deque2_B.toString());
    logger.info(DequeUtil.deque3_B.toString());
    logger.info(DequeUtil.deque4_B.toString());
    logger.info(DequeUtil.deque5_B.toString());
  }
  
  public static void resetCancelThreeList() {
    JedisUtils.setObject("cancel:three:list", new ArrayList(), 44640);
  }
  
  public static List<String> getCancelThreeList() {
    List<String> cancelThreeList;
    Object ob = JedisUtils.getObject("cancel:three:list");
    if (ob == null) {
      cancelThreeList = new ArrayList<>();
      resetCancelThreeList();
    } else {
      cancelThreeList = (ArrayList)ob;
    } 
    return cancelThreeList;
  }
  
  public Map<Integer, Integer> getBookNoMap() {
    if (bookNoMap == null) {
      Map<Integer, Integer> map = new HashMap<>();
      FSchedue fSchedue = new FSchedue();
      fSchedue.setScheduleMonth(Integer.valueOf(Integer.parseInt(DateUtils.getMonth())));
      fSchedue.setScheduleYear(Integer.valueOf(Integer.parseInt(DateUtils.getYear())));
      List<FSchedue> list = this.fSchedueService.findList(fSchedue);
      for (FSchedue bean : list) {
        if (bean.getScheduleDay().intValue() == Integer.parseInt(DateUtils.getDay())) {
          if (bean.getCountA() != null)
            map.put(Integer.valueOf(1), bean.getCountA()); 
          if (bean.getCountB() != null)
            map.put(Integer.valueOf(2), bean.getCountB()); 
          if (bean.getCountC() != null)
            map.put(Integer.valueOf(3), bean.getCountC()); 
          if (bean.getCountD() != null)
            map.put(Integer.valueOf(4), bean.getCountD()); 
          if (bean.getCountE() != null)
            map.put(Integer.valueOf(5), bean.getCountE()); 
        } 
      } 
      bookNoMap = map;
    } 
    return bookNoMap;
  }
}
